﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SourceMaster.UserControls
{
    /// <summary>
    /// ConfirmDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmDialog : Window
    {
        public ConfirmDialog():base()
        {
            InitializeComponent();

            Application.Current.MainWindow.Effect = new BlurEffect
            {
                Radius = 3
            };

            Closing += OnClosing;

            Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Loaded,
                (Action)(InvalidateVisual)
            );

            DataContext = this;

            if (Buttons == null)
                ButtonSet = BtnType.OK_CANCEL;
        }

        public struct BtnType
        {
            public const int OK_CANCEL = 1;
            public const int OK = 2;
        }

        private ObservableCollection<Button> buttons;

        public ObservableCollection<Button> Buttons
        {
            get
            {
                return buttons;
            }
            set
            {
                buttons = value;
                foreach(Button button in buttons)
                {
                    button.Click += (object sender, RoutedEventArgs e) =>
                    {
                        PressedButton = (Button)sender;
                        Close();
                    };
                }
            }
        }

        public int ButtonSet
        {
            set
            {
                if(value == BtnType.OK)
                {
                    Buttons = new ObservableCollection<Button>
                    {
                        new Button
                        {
                            Content = "OK",
                            IsDefault = true,
                            IsCancel = true
                        }
                    };
                }

                if(value == BtnType.OK_CANCEL)
                {
                    Buttons = new ObservableCollection<Button>
                    {
                        new Button
                        {
                            Content = "OK",
                            IsDefault = true
                        },
                        new Button
                        {
                            Content = "Cancel",
                            IsCancel = true
                        }
                    };
                }
            }
        }

        public ObservableCollection<string> foos = new ObservableCollection<string> { "foo", "bar" };
        public ObservableCollection<string> Foos { get { return foos; } }

        public Button PressedButton { get; private set; }

        public string Message
        {
            get { return MessageBlock.Text; }
            set { MessageBlock.Text = value; }
        }

        void OnClosing(object sender, CancelEventArgs e)
        {
            Application.Current.MainWindow.Effect = null;
        }
    }
}
